-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathSepa Algorithm.cpp
77 lines (57 loc) · 1.59 KB
/
Sepa Algorithm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* Sepa Algorithm to find all permutation of a string
Time Complexity: O(n!)
*/
#include <iostream>
#include <string.h>
int permute(std::string &str, int len)
{
int key = len - 1;
int newkey = len - 1;
/* The key value is the first value from the end which
is smaller than the value to its immediate right
*/
while((key > 0) && (str[key] <= str[key-1])) {
key--;
}
key--;
/* If key < 0 the data is in reverse sorted order,
which is the last permutation.
*/
if(key < 0) {
return 0;
}
/* str[key+1] is greater than str[key] because of how key
was found. If no other is greater, str[key+1] is used
*/
newkey = len - 1;
while((newkey > key) && (str[newkey] <= str[key])) {
newkey --;
}
std::swap(str[key], str[newkey]);
/* variables len and key are used to walk through the tail,
exchanging pairs from both ends of the tail. len and
key are reused to save memory
*/
len--;
key++;
/* The tail must end in sorted order to produce the
next permutation.
*/
while(len>key) {
std::swap(str[len], str[key]);
key++;
len--;
}
return 1;
}
int main()
{
std::string s = "0123456789";
/* A short test loop to print each permutation, which are
created in sorted order.
*/
do {
std::cout << s << "\n";
} while(permute(s, 10));
return 0;
}